#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include "request.pb.h"
#include <iostream>
#include <unordered_map>


class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<castle::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<castle::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<castle::AddRquest> AddRquestPtr;
    typedef std::shared_ptr<castle::AddResponse> AddResponsePtr;

    Server(int port)
    :_server(&_baseloop,muduo::net::InetAddress("0.0.0.0",port),
    "Server",muduo::net::TcpServer::kReusePort),
    _dispatcher(std::bind(&Server::onUnknownMessage, this, 
    std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
    _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,
    std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        //注册业务处理函数
        _dispatcher.registerMessageCallback<castle::TranslateRequest>(std::bind(&Server::onTranslate,this,
        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _dispatcher.registerMessageCallback<castle::AddRquest>(std::bind(&Server::onAdd,this,
        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, 
        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        
        _server.setConnectionCallback(std::bind(&Server::onConnect, this, std::placeholders::_1));
    }

    void start()
    {
        _server.start();
        _baseloop.loop();
    }

private:
    std::string translate(const std::string str)
    {
        static std::unordered_map<std::string, std::string> dict_map =
            {
                {"hello", "你好"},
                {"你好", "hello"},
                {"早安", "晚安"}
            };

            auto it = dict_map.find(str);
            if(it == dict_map.end())
            {
                return "查找失败！";
            }
            else
            {
                return it->second;
            }
    }

    void onTranslate(const muduo::net::TcpConnectionPtr& conn,
                        const TranslateRequestPtr& message,
                        muduo::Timestamp)
    {
        std::string req_msg = message->msg();

        std::string rsp_msg = translate(req_msg);

        castle::TranslateResponse resp;
        resp.set_msg(rsp_msg);

        _codec.send(conn,resp);
    }

    void onAdd(const muduo::net::TcpConnectionPtr& conn,
                        const AddRquestPtr& message,
                        muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1+num2;

        castle::AddResponse resp;
        resp.set_result(result);

        _codec.send(conn,resp);
    }

    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn,
                        const MessagePtr& message,
                        muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

    void onConnect(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected())
        {
            LOG_INFO << "新连接建立成功!\n";
        }
        else
        {
            LOG_INFO << "连接即将关闭!\n";
        }
    }
private:   
    muduo::net::EventLoop _baseloop;
    muduo::net::TcpServer _server;//服务器对象
    ProtobufDispatcher _dispatcher;//请求分发器对象，要向其中注册请求处理函数
    ProtobufCodec _codec;//protobuf协议处理器,针对收到的请求数据进行protobuf协议处理
};

int main()
{
    Server server(8005);
    server.start();
    return 0;
}